//
//  FLGuideIndicatorView.m
//  Follow
//
//  Created by liangshangjia on 16/5/9.
//  Copyright © 2016年 Sami-industry. All rights reserved.
//

#import "FLGuideIndicatorView.h"
#import "UIImage+Initial.h"

#define kFLGuideIndicatorCurrentBaseTag 200
#define kFLGuideIndicatorBaseTag 400

#define kFLGuideIndicatorInnerSapcing SM_SIZE(20.f)

@implementation FLGuideIndicatorView {
    int _numOfPage;
    int _currentPage;
}

+ (instancetype)indicatorViewWithNumOfPage:(int)numOfPage
{
    FLGuideIndicatorView *indicatorView = [FLGuideIndicatorView new];
    [indicatorView setValue:@(numOfPage) forKey:@"_numOfPage"];
    [indicatorView configure];
    return indicatorView;
}

- (UIImageView *)currentImageView
{
    return [self viewWithTag:kFLGuideIndicatorCurrentBaseTag + _currentPage];
}

- (void)configure
{

    for (NSInteger i = 0; i < _numOfPage; i ++) {
        
        BOOL isLast = i == _numOfPage - 1;
        BOOL isFirst = i == 0;
        
        UIImageView *imageView  = [[UIImageView alloc]initWithImage:[[UIImage imageNamed:@"空心"] imageWithColor:SM_thumeColor]];
        imageView.tag           = kFLGuideIndicatorBaseTag + i ;
        [self addSubview:imageView];
        
        UIImageView *pImageView = [self viewWithTag:kFLGuideIndicatorBaseTag + i - 1];
        

        
        UIImageView *currentImageView   = [[UIImageView alloc]initWithImage:[[UIImage imageNamed:@"实心"] imageWithColor:SM_thumeColor]];
        currentImageView.tag            = kFLGuideIndicatorCurrentBaseTag + i;
        currentImageView.hidden         = YES;
        [self addSubview:currentImageView];

        for (UIImageView *imageV in @[imageView,currentImageView]) {
            [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
                
                if (isFirst) {
                    make.left.equalTo(@0);
                }else {
                    make.left.equalTo(pImageView.mas_right).offset(kFLGuideIndicatorInnerSapcing);
                }
                
                if (isLast) {
                    make.right.equalTo(@0);
                }
                make.top.bottom.equalTo(@0);
            }];
        }

    }
}



- (void)setCurrentPage:(int)page animated:(BOOL)animated
{

    UIView *view = [self viewWithTag:kFLGuideIndicatorCurrentBaseTag + _currentPage];
    view.hidden = YES;
    
    _currentPage = page;
    UIView *view_current = [self viewWithTag:kFLGuideIndicatorCurrentBaseTag + _currentPage];
    view_current.hidden = NO;
    
    if (animated) {
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        animation.fromValue         = @(1.f);
        animation.toValue           = @(2.f);
        animation.duration          = .18;
        animation.autoreverses      = YES;
        [view_current.layer addAnimation:animation forKey:nil];
    }
    
    
    
}


@end
